/*==============================================================================
 * Copyright 2011 zod.yslin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Author: zod.yslin
 * Email: 
 * File Name: encode_utf8.java
 * Description: 
 * 
 * 帮助ADT改进DDMS中的Logcat中文乱码问题
 * 作者: Android开发网原创 时间: 2011-03-07
 * 有的时候我们调试Android应用可能涉及中文内容，但是在DDMS的Logcat下显示中文时为乱码，这里大家可以通过自己编译SDK来解决，有关编译Android SDK方法可以参考如何编译Windows平台的Android SDK 下面一起看下哪个代码存在问题吧。
 * 在Android源码DDMS中我们找到 MultiLineReceiver 这个类，对应GIT开源在development/tools/ddms/libs/ddmuilib/src/com/android/ddmuilib/，最主要的就是有关String实例化时最后一个参数，看到ISO-8859-1了吧，我们将这个换成gb2312就可以很好的显示简体中文了，繁体嘛可以考虑big5这种编码等等了，当然了Android123推荐大家使用UTF-8这种兼容性最好的。
 * Edit History: 
 *   2011-09-01    File created.
 *=============================================================================*/

 public abstract class MultiLineReceiver implements IShellOutputReceiver {

     public final void addOutput(byte[] data, int offset, int length) {
         if (isCancelled() == false) {
             String s = null;
             try {
                 s = new String(data, offset, length, "ISO-8859-1"); //问题在这里，ISO-8859-1就是Latin-1我们俗称西欧语言
             } catch (UnsupportedEncodingException e) {
                 // normal encoding didn't work, try the default one
                 s = new String(data, offset,length);
             }

             // ok we've got a string
             if (s != null) {
                 // if we had an unfinished line we add it.
                 if (mUnfinishedLine != null) {
                     s = mUnfinishedLine + s;
                     mUnfinishedLine = null;
                 }


                 mArray.clear();
                 int start = 0;
                 do {
                     int index = s.indexOf("\r\n", start);

                     // if \r\n was not found, this is an unfinished line
                     // and we store it to be processed for the next packet
                     if (index == -1) {
                         mUnfinishedLine = s.substring(start);
                         break;
                     }


                     String line = s.substring(start, index);
                     if (mTrimLines) {
                         line = line.trim();
                     }
                     mArray.add(line);

                     // move start to after the \r\n we found
                     start = index + 2;
                 } while (true);

                 if (mArray.size() > 0) {

                     String[] lines = mArray.toArray(new String[mArray.size()]);

                     // send it for final processing
                     processNewLines(lines);
                 }
             }
         }
     }
 }
